package com.cbank.platform.interceptor;

import com.cbank.service.AdminUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截所有请求
 */
public class CommonInterceptor implements HandlerInterceptor {
    org.apache.log4j.Logger log= Logger.getLogger(CommonInterceptor.class);
    @Autowired
    private AdminUserService adminUserService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("=======================preHandle==========================");
        log.info("====================="+request.getRequestURI()+"请求开始============================");
        String token=request.getHeader("token");
        String userId=request.getHeader("userId");
        userId="1";
        token="asd";
        if(token.equals(adminUserService.findById(Integer.valueOf(userId)))){
            return true;
        }else {
            request.getRequestDispatcher("/admin/reloadLogin").forward(request,response);
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("=======================postHandle==========================");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("=======================afterCompletion==========================");
    }

}
